﻿#region Using Directives

using System;
using System.Web.Security;
using System.Collections.Generic;

using QuickDev.Common.Serialization;
using QuickDev.Common.Utils;

using QuickDev.Model;
using QuickDev.SQLServerDAL;
using QuickDev.Caching;

#endregion

namespace QuickDev.BLL
{
    /// <summary>
    /// BLL Layer For dbo.SYS_ServiceConfiguration.
    /// </summary>
    public class ServiceConfigurationBLL
    {
        #region ServiceConfigurationBLL.IsExist( string service , string type )
        /// <summary>
        /// public static bool IsExist( string service , string type )
        /// </summary>
        /// <param name="service">service</param>
        /// <param name="type">type</param>
        /// <returns>bool Existed</returns>
        public static bool IsExist( string service, string type )
        {
            Validation.ValidateStringArgumentIsNotNullOrEmptyString( service, "QuickDev.BLL.ServiceConfigurationBLL.IsExist() Method -- service" );
            Validation.ValidateStringArgumentIsNotNullOrEmptyString( type, "QuickDev.BLL.ServiceConfigurationBLL.IsExist() Method -- type" );

            return ServiceConfigurationDAL.IsExist( service, type );
        }

        #endregion

        #region ServiceConfigurationBLL.Add( string service, string type, string value, int createBy ) Method

        /// <summary>
        /// public static int Add( string service, string type, string value, int createBy )
        /// </summary>
        /// <param name="service">service</param>
        /// <param name="type">type</param>
        /// <param name="value">value</param>
        /// <param name="createdBy">createdBy</param>
        /// <returns>ServiceConfiguration model</returns>
        public static ServiceConfiguration Add( string service, string type, string value, int createBy )
        {
            Validation.ValidateStringArgumentIsNotNullOrEmptyString( service, "QuickDev.BLL.ServiceConfigurationBLL.Add() Method -- service" );
            Validation.ValidateStringArgumentIsNotNullOrEmptyString( type, "QuickDev.BLL.ServiceConfigurationBLL.Add() Method -- type" );
            Validation.ValidateStringArgumentIsNotNullOrEmptyString( value, "QuickDev.BLL.ServiceConfigurationBLL.Add() Method -- value" );

            int newConfigurationId = ServiceConfigurationDAL.Add( service, type, value, createBy );

            if ( newConfigurationId > 0 )
            {
                ServiceConfigurationCache.RemoveAllCollectionFromCache();

                return ServiceConfigurationCache.GetModel( newConfigurationId );
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region ServiceConfigurationBLL.Update( int configurationId, string service, string type, string value, int updateBy ) Method

        /// <summary>
        /// public static bool Update( int configurationId, string service, string type, string value, int updateBy )
        /// </summary>
        /// <param name="configurationId">configurationId</param>
        /// <param name="service">service</param>
        /// <param name="type">type</param>
        /// <param name="value">value</param>
        /// <param name="updateBy">updateBy</param>
        /// <returns>ServiceConfiguration model</returns>
        public static ServiceConfiguration Update( int configurationId, string service, string type, string value, int updateBy )
        {
            Validation.ValidateIntegerIsGreaterThanZero( configurationId, "QuickDev.BLL.ServiceConfigurationBLL.Update() Method -- configurationId" );
            Validation.ValidateStringArgumentIsNotNullOrEmptyString( service, "QuickDev.BLL.ServiceConfigurationBLL.Update() Method -- service" );
            Validation.ValidateStringArgumentIsNotNullOrEmptyString( type, "QuickDev.BLL.ServiceConfigurationBLL.Update() Method -- type" );
            Validation.ValidateStringArgumentIsNotNullOrEmptyString( value, "QuickDev.BLL.ServiceConfigurationBLL.Update() Method -- value" );

            if ( ServiceConfigurationDAL.Update( configurationId, service, type, value, updateBy ) )
            {
                ServiceConfigurationCache.RemoveAllCollectionFromCache();
                ServiceConfigurationCache.RemoveModelFromCache( configurationId );

                return ServiceConfigurationCache.GetModel( configurationId );
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region ServiceConfigurationBLL.Update(int configurationId, string status, int updateBy) Method

        /// <summary>
        /// public bool Update(int configurationId, string status, int updateBy)
        /// </summary>
        /// <param name="configurationId">configurationId</param>
        /// <param name="status">status</param>
        /// <param name="updateBy">updateBy</param>
        /// <returns>ServiceConfiguration model</returns>
        public static ServiceConfiguration Update( int configurationId, int status, int updateBy )
        {
            Validation.ValidateIntegerIsGreaterThanZero( configurationId, "QuickDev.BLL.ServiceConfigurationBLL.Update -- configurationId" );

            if ( ServiceConfigurationDAL.Update( configurationId, status, updateBy ) )
            {
                ServiceConfigurationCache.RemoveAllCollectionFromCache();
                ServiceConfigurationCache.RemoveModelFromCache( configurationId );

                return ServiceConfigurationCache.GetModel( configurationId );
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region ServiceConfigurationBLL.Add( string service , object obj , int createBy ) Method

        /// <summary>
        /// public static int Add( string service , object obj , int createBy )
        /// </summary>
        /// <param name="service">service</param>
        /// <param name="obj">obj</param>
        /// <param name="createdBy">createdBy</param>
        /// <returns>ServiceConfiguration model</returns>
        public static ServiceConfiguration Add( string service, object obj, int createBy )
        {
            Validation.ValidateStringArgumentIsNotNullOrEmptyString( service, "QuickDev.BLL.ServiceConfigurationBLL.Add() Method -- service" );
            Validation.ValidateArgumentIsSerializable( obj, "QuickDev.BLL.ServiceConfigurationBLL.Add() Method -- obj" );

            Serializer serializer = new Serializer();
            string xml = serializer.SerializeObject( obj, obj.GetType() );


            int newConfigurationId = ServiceConfigurationDAL.Add( service, obj.GetType().AssemblyQualifiedName, xml, createBy );

            if ( newConfigurationId > 0 )
            {
                ServiceConfigurationCache.RemoveAllCollectionFromCache();
                ServiceConfigurationCache.GetModel( service );

                return ServiceConfigurationCache.GetModel( newConfigurationId );
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region ServiceConfigurationBLL.Update( int configurationId, string service, object obj, int updateBy ) Method

        /// <summary>
        /// public static bool Update( int configurationId, string service, object obj, int updateBy )
        /// </summary>
        /// <param name="configurationId">configurationId</param>
        /// <param name="service">service</param>
        /// <param name="obj">obj</param>
        /// <param name="updateBy">updateBy</param>
        /// <returns>ServiceConfiguration model</returns>
        public static ServiceConfiguration Update( int configurationId, string service, object obj, int updateBy )
        {
            Validation.ValidateIntegerIsGreaterThanZero( configurationId, "QuickDev.BLL.ServiceConfigurationBLL.Update() Method -- configurationId" );
            Validation.ValidateStringArgumentIsNotNullOrEmptyString( service, "QuickDev.BLL.ServiceConfigurationBLL.Update() Method -- service" );
            Validation.ValidateArgumentIsSerializable( obj, "QuickDev.BLL.ServiceConfigurationBLL.Update() Method -- obj" );

            Serializer serializer = new Serializer();
            string xml = serializer.SerializeObject( obj, obj.GetType() );

            if ( ServiceConfigurationDAL.Update( configurationId, service, obj.GetType().AssemblyQualifiedName, xml, updateBy ) )
            {
                ServiceConfigurationCache.RemoveAllCollectionFromCache();
                ServiceConfigurationCache.RemoveModelFromCache( configurationId );

                return ServiceConfigurationCache.GetModel( configurationId );
            }
            else
            {
                return null;
            }
        }
        #endregion

    }
}

